package com.glorysoft.xztpda.bindingadapter

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import androidx.databinding.BindingAdapter
import com.glorysoft.xztpda.entity.EdcDataEDCSETLISTEDCSETBINLINELIST
import java.lang.NumberFormatException
import java.text.NumberFormat

/**
 * FileName: NumEditextAdapter
 * Author: XJH
 * Date: 2022/4/28 18:34
 * Description:
 * History:
 */
object EdcEdiTextAdapter {

    @BindingAdapter(value = ["edcCheck"], requireAll = false)
    @JvmStatic
    fun setEdcEditText(editText: EditText, item: EdcDataEDCSETLISTEDCSETBINLINELIST) {
        editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                if (s.toString().isNotEmpty()) {
                    try {
                        val numberFormat = NumberFormat.getInstance()
                        numberFormat.maximumFractionDigits = 2
                        val binQty = s.toString().toFloat()
                        val mainqty = item.MAINQTY
                        val result =
                            (numberFormat.format((binQty.div(mainqty.toFloat()) * 100)))
                        item.check = "$result%"
                    }catch (e : Exception){
                        item.check = "0.0%"
                    }
                }else{
                    item.check = "0.0%"
                }

            }

        })
    }
}